package com.zuimei.zuimei.activities;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.base.MySwipeBackActivity;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.utils.ScreenUtil;
import com.zuimei.zuimei.utils.ShareSdkUtil;
import com.zuimei.zuimei.widgets.DownLoadDialog;
import com.zuimei.zuimei.widgets.MyScrollView;
import com.zuimei.zuimei.widgets.XCFlowLayout;

import butterknife.Bind;
import butterknife.OnClick;

public class Item2DescActivity extends MySwipeBackActivity {

    @Bind(R.id.iv_avatar)
    ImageView iv_avatar;
    @Bind(R.id.tv_flowers)
    TextView tv_flowers;
    @Bind(R.id.tv_user_name)
    TextView tv_user_name;
    @Bind(R.id.ibtn_back)
    ImageView ibtn_back;
    @Bind(R.id.iv_edxq_sm)
    ImageView iv_edxq_sm;
    @Bind(R.id.tv_edxq_title)
    TextView tv_edxq_title;
    @Bind(R.id.tv_edxq_sub)
    TextView tv_edxq_sub;
    @Bind(R.id.tv_report)
    TextView tv_report;
    @Bind(R.id.bt_edxq_fav)
    Button bt_edxq_fav;
    @Bind(R.id.bt_edxq_share)
    Button bt_edxq_share;
    @Bind(R.id.bt_edxq_down)
    Button bt_edxq_down;
    @Bind(R.id.tv_desc)
    TextView tv_desc;
    @Bind(R.id.sv_content_container)
    MyScrollView sv_content_container;
    /*@Bind(R.id.ll_tags_container)
    LinearLayout ll_tags_container;*/
    @Bind(R.id.ll_desc_img_container)
    LinearLayout ll_desc_img_container;
    @Bind(R.id.ibtn_app)
    ImageButton ibtn_app;
    @Bind(R.id.tv_edxq_tui)
    TextView tv_edxq_tui;
    @Bind(R.id.iv_flower)
    ImageView iv_flower;
    @Bind(R.id.ll_download)
    LinearLayout ll_download;
    @Bind(R.id.tv_career)
    TextView tv_career;
    @Bind(R.id.flowlayout)
    XCFlowLayout flowlayout;
    @Bind(R.id.ll_btn_container)
    LinearLayout ll_btn_container;
    @Bind(R.id.ibtn_fav)
    ImageButton ibtn_fav;
    @Bind(R.id.ibtn_share)
    ImageButton ibtn_share;
    @Bind(R.id.ibtn_down)
    ImageButton ibtn_down;
    @Bind(R.id.rl_head_container)
    RelativeLayout rl_head_container;
    @Bind(R.id.ll_head_container)
    LinearLayout ll_head_container;
    private FindAppItem2Bean mApp;
    private int y;
    private int mY;
    private int mShowY;

    @Override
    public int getLayoutId() {
        return R.layout.activity_item2_desc;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mY = ScreenUtil.getSrceenHeight(getBaseContext());
        int[] position = new int[2];
        ibtn_app.getLocationOnScreen(position);
        y = position[1];
    }

    @Override
    protected void init() {
        ll_head_container.measure(0, 0);
        rl_head_container.measure(0, 0);
        //ibtn_back.measure(0, 0);
        mShowY = ll_head_container.getMeasuredHeight() + rl_head_container.getMeasuredHeight();
        mApp = (FindAppItem2Bean) getIntent().getSerializableExtra("app");
        tv_edxq_title.setText(mApp.getTitle());
        tv_edxq_sub.setText(mApp.getSub_title());
        tv_user_name.setText(mApp.getAuthor_name());
        Picasso.with(this)
                .load(mApp.getAuthor_avatar_url())
                .config(Bitmap.Config.RGB_565)
                .into(iv_avatar);
        Picasso.with(this)
                .load(mApp.getIcon_image())
                .config(Bitmap.Config.RGB_565)
                .into(iv_edxq_sm);
        tv_career.setText(mApp.getAuthor_career());
        tv_flowers.setText(mApp.getAuthor_flowers() + "");
        tv_desc.setText(Html.fromHtml(mApp.getDescription()));
        ibtn_app.measure(0, 0);
        if (mApp.getAll_images().size() != 0) {
            for (int i = 0; i < mApp.getAll_images().size(); i++) {
                ImageView imageView = new ImageView(this);
                Picasso.with(this)
                        .load(mApp.getAll_images().get(i))
                        .config(Bitmap.Config.RGB_565)
                        .into(imageView);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup
                        .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.topMargin = 20;
                imageView.setLayoutParams(params);
                ll_desc_img_container.addView(imageView);
            }
        }
        if (mApp.getTags().size() != 0) {
            for (int i = 0; i < mApp.getTags().size(); i++) {
                CardView cardView = new CardView(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams
                        .WRAP_CONTENT);
                cardView.setRadius(30);
                TextView textView = new TextView(this);
                params.leftMargin = 20;
                params.topMargin = 1;
                params.bottomMargin = 10;
                params.rightMargin = 1;
                cardView.setLayoutParams(params);
                textView.setLayoutParams(params);
                textView.setPadding(50, 10, 50, 10);
                textView.setTextColor(Color.BLACK);
                textView.setText(mApp.getTags().get(i).getTitle());
                cardView.addView(textView);
                flowlayout.addView(cardView);
            }
        }


    }

    @Override
    protected void loadData() {
        sv_content_container.setOnScrollListener(mShowY + ibtn_back.getMeasuredHeight(), mShowY,
                new MyScrollView.OnScrollListener() {
                    @Override
                    public void showAppBtn() {
                        ibtn_app.animate().translationYBy(y).y(mY).setDuration(500)
                                .setInterpolator(new BounceInterpolator()).start();
                    }

                    @Override
                    public void hideAppBtn() {
                        ibtn_app.animate().translationYBy(mY).y(y).setDuration(500)
                                .setInterpolator(new BounceInterpolator()).start();
                    }

                    @Override
                    public void upShow() {
                        ll_btn_container.setVisibility(View.INVISIBLE);
                        ibtn_fav.setVisibility(View.VISIBLE);
                        ibtn_share.setVisibility(View.VISIBLE);
                        ibtn_down.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void downShow() {
                        //滑动到与回退按钮底部
                        ibtn_fav.setVisibility(View.INVISIBLE);
                        ibtn_share.setVisibility(View.INVISIBLE);
                        ibtn_down.setVisibility(View.INVISIBLE);
                        ll_btn_container.setVisibility(View.VISIBLE);
                    }
                });
    }

    @OnClick({R.id.ibtn_back, R.id.ll_download, R.id.bt_edxq_down, R.id.ibtn_down, R.id
            .bt_edxq_share, R.id.ibtn_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.ll_download:
            case R.id.bt_edxq_down:
            case R.id.ibtn_down:
                download();
                break;
            case R.id.bt_edxq_share:
            case R.id.ibtn_share:
                new ShareSdkUtil().showShare(this);
                break;
        }
    }

    private void download() {
        DownLoadDialog dialog = new DownLoadDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", mApp.getTitle());
        bundle.putString("size", mApp.getSize());
        if (mApp.getDownload_urls().size() != 0) {
            for (int i = 0; i < mApp.getDownload_urls().size(); i++) {
                String name = mApp.getDownload_urls().get(i).getName();
                if ("豌豆荚".equals(name)) {
                    bundle.putString("wandoujia", mApp.getDownload_urls().get(i)
                            .getUrl());
                }
                if ("Google Play".equals(name)) {
                    bundle.putString("wandoujia", mApp.getDownload_urls().get(i)
                            .getUrl());
                }
                if ("直接".equals(name)) {
                    bundle.putString("direct", mApp.getDownload_urls().get(i)
                            .getUrl());
                }
            }
        }
        dialog.setArguments(bundle);
        dialog.show(getFragmentManager(), "download_dialog");
    }

}
